//
//  WDWorderOrdMainVehCurrentRemarkImageView.m
//  wd-serviceapp
//
//  Created by Bin Tang on 2024/9/2.
//

#import "WDWorderOrdMainVehCurrentRemarkImageView.h"
#import "WDGoutReviewVehicleRemarkSingleImageView.h"
#import "WDWorderOrderMaintenanceVehicleViewModel.h"
#import "EDSPublishDynamicImageVideoView.h"



@interface WDWorderOrdMainVehCurrentRemarkImageView ()

@property (nonatomic, strong) WDWorderOrderMaintenanceVehicleViewModel *viewModel;

@property (nonatomic, strong) EDSPublishDynamicImageVideoView *imageVideoView; ///< 发布动态的视图

@property (nonatomic, strong) UILabel *titleLabel;

/// 描述
@property (nonatomic, strong) UILabel *bottomDescLabel;

@end

@implementation WDWorderOrdMainVehCurrentRemarkImageView

- (void)initWithSubView {
    [super initWithSubView];
    
//    self.backgroundColor = [UIColor orangeColor];
    
    [self initWithTitleLabel];
}

- (void)initWithTitleLabel {
    if (!_titleLabel) {
        CGRect frame = CGRectMake(25, 2, self.width - 32, 20);
        _titleLabel = [UILabel createWithLabel:self frame:frame font:[UIFont systemFontOfSize:14 weight:UIFontWeightRegular] propertiesBlock:^(UILabel * _Nonnull label) {
            label.text = @"备注图片";
            label.textColor = [UIColor colorWithHexString:@"#000000"];
        }];
        CGRect lframe = CGRectMake(20, 0, 2, 10);
        CALayer *line = [CALayer drawLine:self frame:lframe lineColor:kThemeColor];
        line.centerY = self.titleLabel.centerY;
    }
}

- (void)setTitle:(NSString *)title {
    _titleLabel.text = title;
}

/// 更新图片
/// - Parameter model: model description
- (void)updateWithSignImage:(TYZCommonCellModel *)model {
    NSInteger index = model.index - 100;
    WDGoutReviewVehicleRemarkSingleImageView *view = [self.viewList objectOrNilAtIndex:index];
    if (view) {
        [view updateWithImage:model.image];
    }
}

/**
  动态发布的视图
 */
- (void)initWithImageVideoView
{
    if (!_imageVideoView)
    {
        UIView *preView = self.viewList.lastObject;
        float y = preView.bottom + 10;
        if (self.viewList.count == 0) {
            y = _titleLabel.bottom + 8;
        }
        
        CGRect frame = CGRectMake(20, y, self.width - 40, self.viewModel.imageHeight);
        _imageVideoView = [[EDSPublishDynamicImageVideoView alloc] initWithFrame:frame imageVideoNum:self.viewModel.completeRepairImageNum imageWidth:self.viewModel.imageWidth imageHeight:self.viewModel.imageHeight];
        [self addSubview:_imageVideoView];
//        self.imageVideoView.backgroundColor = [UIColor orangeColor];
        
        // 点击视频图片
        @weakify(self);
        _imageVideoView.touchVideoImgViewblock = ^{
            @strongify(self);
            if (self.touchVideoImgViewblock)
            {
                self.touchVideoImgViewblock();
            }
        };
        // 删除图片
        _imageVideoView.deleteImageBlock = ^(NSInteger index) {
            @strongify(self);
            [self.viewModel.remarkImages removeObjectAtIndex:index];
            [self updateWithViewData:nil];
        };
        
        // 点击添加图片或者视频
        [[_imageVideoView.btnAddImage rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
            @strongify(self);
            if (self.touchChoiceImageVideoBlock) {
                self.touchChoiceImageVideoBlock();
            }
        }];
    }
    
    _imageVideoView.height = self.viewModel.remarkImagesHeight;
    
    // 图片
    self.imageVideoView.videoImgView.hidden = YES;
    NSInteger count = self.viewModel.remarkImages.count;
    
    for (int i=0; i<self.viewModel.completeRepairImageNum; i++)
    {
        EDSPublishDynamicImageView *view = self.imageVideoView.imageList[i];
        if (i<count)
        {
            EDSImageVideoModel *model = self.viewModel.remarkImages[i];
            view.image = model.srcImage;
            view.hidden = NO;
            if (model.type == 1) {
                view.showPlayBtn = YES;
            } else {
                view.showPlayBtn = NO;
            }
        }
        else
        {
            view.hidden = YES;
        }
    }
    if (count >= self.viewModel.completeRepairImageNum)
    {
        self.imageVideoView.btnAddImage.hidden = YES;
    }
    else
    {
        EDSPublishDynamicImageView *view = self.imageVideoView.imageList[count];
        self.imageVideoView.btnAddImage.frame = view.frame;
        self.imageVideoView.btnAddImage.hidden = NO;
    }
    
    if (self.touchUpdateViewHeightBlock) {
        self.touchUpdateViewHeightBlock();
    }
    
}

- (void)initWithBottomDescLabel {
    if (!_bottomDescLabel) {
        CGRect frame = CGRectMake(20, 0, self.width - 40, 40);
        _bottomDescLabel = [UILabel createWithLabel:self frame:frame font:[UIFont systemFontOfSize:12 weight:UIFontWeightRegular] propertiesBlock:^(UILabel * _Nonnull label) {
            label.numberOfLines = 2;
        }];
    }
    if (self.viewModel) {
        NSString *str = [NSString stringWithFormat:@"请上传至少3张维修过程照片和故障部位维修后照片，最多上传%zd张", self.viewModel.completeRepairImageNum];
        NSMutableAttributedString *mut = [EDSUtility attributeTitle:@"" titleColor:[UIColor colorWithHexString:@"#8B8E9D"] titleFont:[UIFont systemFontOfSize:13 weight:UIFontWeightRegular] value:str valueColor:[UIColor colorWithHexString:@"#8B8E9D"] valueFont:[UIFont systemFontOfSize:13 weight:UIFontWeightRegular]];
        self.bottomDescLabel.attributedText = mut;
        self.bottomDescLabel.top = _imageVideoView.bottom + 10;
        self.height = self.bottomDescLabel.bottom;
    }
}

- (void)updateWithViewData:(id)data {
    if (data) {
        self.viewModel = data;
    }
    
    if (self.viewList.count > 0) {
        [self initWithImageVideoView];
        
        [self initWithBottomDescLabel];
        self.height = self.bottomDescLabel.bottom;
        
        if (self.touchUpdateViewHeightBlock) {
            self.touchUpdateViewHeightBlock();
        }
        return;
    }
    
    [self initWithImageVideoView];
    
    [self initWithBottomDescLabel];
    
//    self.height = _imageVideoView.bottom;
    self.height = self.bottomDescLabel.bottom;
    if (self.touchUpdateViewHeightBlock) {
        self.touchUpdateViewHeightBlock();
    }
}


@end
